//
//  DetailBuyyerViewController.m
//  SmartCoin
//
//  Created by apple on 2018/3/14.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "DetailBuyyerViewController.h"
#import "DetailBuyyerTableViewCell.h"
#import "SellerMineViewController.h"
static NSString *DetailBuyyerTableViewCellIndentifier = @"DetailBuyyerTableViewCellIndentifier";
@interface DetailBuyyerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,retain)UITableView *mainView;
@property (nonatomic ,retain)NSMutableArray *storelist;
@end

@implementation DetailBuyyerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewLoad];
}

-(void)viewLoad{
    self.view.backgroundColor = bggrayColor;
    _storelist = [NSMutableArray array];
    [self navigationLoad];
    [self mainViewLoad];
    [self dataLoad];
}

//-(void)personinfoload{
//    NetWorkModel *netModel = [[NetWorkModel alloc] init];
//    WEAKSELF;
//    netModel.sendMessage = ^(NSString *result) {
//        if ([result isEqual:@"success"]) {
//            [weakSelf dataLoad];
//        }
//        else{
//
//        }
//    };
//    [netModel getUserMoneyinfoByUser:cxsingler.openid];
//
//}

-(void)dataLoad{
    NetWorkModel *netModel = [[NetWorkModel alloc] init];
    WEAKSELF;
    netModel.sendMessage = ^(NSString *result) {
        if ([result isEqual:@"success"]) {
            NSMutableArray *list = [cxsingler.ArraryData mutableCopy];
            if (list.count > 0) {
            for (NSDictionary *part in list) {
                [weakSelf.storelist addObject:part];
            }
                dispatch_async(dispatch_get_main_queue(), ^{
                   
//                    if (weakSelf.storelist.count == 1) {
////                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                           [weakSelf jumpWithRow:0];
////                        });
//                    }
                     [weakSelf.mainView reloadData];
                });
            }
        }
        else{
            
        }
    };
    [netModel getBossInfoListByUser:cxsingler.loginrequest[@"mobile"]];
}

-(void)navigationLoad{
    UIView *navigationBG = [[UIView alloc] initWithFrame:CGRectMake(0, cxsingler.topdis -  cxsingler.navitop, cx_wid, 140)];
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.frame = CGRectMake(0, 0, cx_wid, 152);
    layer.colors = @[(id)[CXSinglerModel colorWithHex:@"#FF7F5A" alpha:1].CGColor,(id)[CXSinglerModel colorWithHex:@"#FE4438" alpha:1].CGColor];
    layer.startPoint = CGPointMake(0, 0);
    layer.endPoint = CGPointMake(1, 0);
    [navigationBG.layer addSublayer:layer];
    
    
    UIButton *popButton = [[UIButton alloc] initWithFrame:CGRectMake(0, cxsingler.topdis, 44, 44)];
    UIButton *centerBtn = [[UIButton alloc] initWithFrame:CGRectMake(44, cxsingler.topdis, cx_wid - 88, 44)];
    centerBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [centerBtn setTitle:@"我的店铺" forState:UIControlStateNormal];
    [popButton setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [popButton addTarget:self action:@selector(popbtn:) forControlEvents:UIControlEventTouchUpInside];
//    [navigationBG addSubview:popButton];
    [navigationBG addSubview:centerBtn];
    [self.view addSubview:navigationBG];
}

-(void)mainViewLoad{
    if (!_mainView) {
        _mainView = [[UITableView alloc ] initWithFrame:CGRectMake(0, cxsingler.topdis + 44 , cx_wid, cx_hit - cxsingler.topdis - 44 - cxsingler.bottomdis)];
        _mainView.estimatedRowHeight = 0;
        _mainView.delegate = self;
        _mainView.dataSource = self;
        _mainView.backgroundColor = [UIColor clearColor];
        [_mainView registerClass:[DetailBuyyerTableViewCell class] forCellReuseIdentifier:DetailBuyyerTableViewCellIndentifier];
        _mainView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_mainView];
    }
}

#pragma mark--uitableview代理开始
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _storelist.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 122;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    DetailBuyyerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailBuyyerTableViewCellIndentifier];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableDictionary *partData = [_storelist[row] mutableCopy];
    cell.storelabel.text =  [NSString stringWithFormat:@"%@",partData[@"shopName"]];
    cell.namelabel.text =  [NSString stringWithFormat:@"%@",partData[@"name"]];
    cell.joblabel.text = [NSString stringWithFormat:@"%@",partData[@"jobTitle"]];
    [cell.header sd_setBackgroundImageWithURL:[NSURL URLWithString:cxsingler.personData[@"avatar"]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"logo1"]];
    WEAKSELF;
    cell.btnTaped = ^{
        [weakSelf jumpWithRow:row];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    [self jumpWithRow:row];
}
#pragma mark--uitableview代理结束

#pragma mark--跳转进个人详情
-(void)jumpWithRow:(NSInteger)row{
    NSDictionary *partdata = _storelist[row];
    cxsingler.staffid = [NSString stringWithFormat:@"%@",partdata[@"staffId"]];
    cxsingler.shopid = [NSString stringWithFormat:@"%@",partdata[@"shopId"]];
    cxsingler.jobname = [NSString stringWithFormat:@"%@",partdata[@"jobTitle"]];
    cxsingler.companyname = [NSString stringWithFormat:@"%@",partdata[@"shopName"]];
    SellerMineViewController *nextVC = [[SellerMineViewController alloc] initWithshopID:[NSString stringWithFormat:@"%@",partdata[@"staffId"]]];
    [self.navigationController pushViewController:nextVC animated:YES];
}

#pragma mark--点击
-(void)popbtn:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
